from flask import Blueprint, jsonify
from models import db, Events
from api.user import token_required  # 导入 JWT 验证装饰器

appeals_bp = Blueprint('appeals', __name__)

@appeals_bp.route('/appeals/<int:user_id>', methods=['GET'])
@token_required  # 添加 JWT 验证
def get_appeals(user_id):
    """根据用户 ID 获取所有的申诉事件"""
    appealed_events = Events.query.filter(Events.reported_user_id == user_id, Events.appeal_status.isnot(None)).all()
    event_list = [{
        "event_id": event.event_id,
        "reporter_id": event.reporter_id,
        "reported_user_id": event.reported_user_id,
        "timestamp": event.timestamp,
        "location": event.location,
        "violation_type": event.violation_type,
        "description": event.description,
        "scene_image": event.scene_image,
        "video": event.video,
        "report_status": event.report_status,
        "appeal_status": event.appeal_status,
        "estimated_processing_time": event.estimated_processing_time,
        "reporter_has_read": event.reporter_has_read,
        "reporter_has_favorited": event.reporter_has_favorited,
        "reported_user_has_favorited": event.reported_user_has_favorited,
        "reported_user_has_read": event.reported_user_has_read,
        "is_reported_user_processed": event.is_reported_user_processed
    } for event in appealed_events]

    return jsonify({"appealed_events": event_list}), 200
